<?php
namespace Web\Controller;
use Think\Controller;

class ProfileController extends BaseController {
    public function index(){
         if(IS_POST){
              $rules = array(
                array('company','require','公司名称必填！'),
                array('phone','require','电话号码必填！'),
                array('email','require','Email必填！')
            );

            $Member = M('member');
            if (!$Member->validate($rules)->create()){
                exit('<script>parent.notify("error", "提示", "' . $Member->getError() . '");</script>');
            }else{
                $Member->userid = (I('userid'));
                $Member->updated_at = date('Y-m-d H:i:s');
                $ins = $Member->where(array('userid'=>$Member->userid))->save();
            }

            if($ins !== false){
            
                $data['company'] = I('company');
                M('member_info')->where(array('user_id'=>I('userid')))->save($data);
                //公司名称中增加公司名记录
                $add_company['company_name'] = $data['company'];
                $count = M('member_company')->where($add_company)->count();
                
                
                if($count == 0){
                    $add_company['is_show'] = 1;
                    M('member_company')->add($add_company);
                    
                }
                echo '<script>parent.notify("success", "提示", "保存成功！");parent.location="' . U('index') . '";</script>';
            } else {
                echo '<script>parent.notify("error", "提示", "保存失败！");</script>';
            }
             
         }else{
             
              $user_id = session(C('SESSION_ADMIN_ID'));
                $where['userid'] =  $user_id;
                
                $data= M('member')->alias('m')
                ->join('left join ly_member_info mi ON m.userid= mi.user_id')
                ->where($where)
                ->field("m.*,mi.company")
                ->find();
                $this->assign('data',$data);
                
                 $companys = M('member_company')->distinct(true)->order('company_name')->getField('company_name', true);

                 $this->assign('companys', $companys);
                
                $this->display();
                     
                 }
       
    }
    public function message(){

        $where['to_userid'] =  session(C('SESSION_ADMIN_ID'));
        
        
        $count = M('member_message')->where($where)->count();
        $Page  = $this->getpage($count,30);
        
        $list = M('member_message')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->where($where)
            ->order('id desc')
            ->select();
        
        $show  = $this->bootstrap_page_style($Page->show());
        $this->assign('page',$show);
        $this->assign('list',$list);
        $this->display();
    }
    
    public function setMessage($id,$type){
       
        $user_id = session(C('SESSION_ADMIN_ID'));
        $where['id'] =  $id;
        
        $data = M('member_message')->where($where)->find();
        
        $update_data['type_result'] = $type;
        $update_data['is_read'] = 1;
        M('member_message')->where($where)->save($update_data);
        
        //如果是同意
        if($type == 1){
           $add_data['project_id'] = $data['value']; 
           $add_data['user_id'] = $user_id;
           $count = M('project_user')-> where($add_data)->count();
           if($count == 0){
                $add_data['created_at'] = date('Y-m-d H:i:s');
                 M('project_user')->add($add_data);
                 echo "2";
           }else{
               echo "1";
           } 
        }else{
            echo "2";
        }
       
    }
    
    
    
    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     'user/'; // 设置附件上传（子）目录
        $upload->saveName = session(C('SESSION_ADMIN_ID'));
        $upload->autoSub = false;
        $upload->replace = true;
        
        // 上传文件 
        $info   =   $upload->upload();
        echo __ROOT__;
        if(!$info) {// 上传错误提示错误信息
            echo "0";
        }else{// 上传成功
            $path =  '/Uploads/'.$upload->savePath.$info['input-file']['savename'];
            $update['image_url'] = $path;
            $where['userid'] = session(C('SESSION_ADMIN_ID'));
            M('member')->where($where)->save($update);
            echo "1";
        }
    }

    public function getMsg($id=0){
        $Message = M('member_message');
        $map['id'] = $id;
        $map['to_userid'] = session(C('SESSION_ADMIN_ID'));
        $Message->where($map)->setField('is_read', 1);
        $content = $Message->where($map)->getField('content');
        echo $content;
        
        
    }

    //删除消息
	public function delMsg(){
        $ids = I('ids/a');
        $map['id'] = array('in', $ids);
        M('member_message')->where($map)->delete();
        echo '1';
    }

    //接受邀请
    public function accept($id=0){
        $map['id'] = $id;
        $map['to_userid'] = session(C('SESSION_ADMIN_ID'));
        $Message = M('member_message');
        $vo = $Message->where($map)->find();

        $ProjectUser = M('project_user');
        $cond['project_id'] = $vo['value'];
        $cond['user_id'] = $vo['to_userid'];

        $pid = $ProjectUser->where($cond)->getField('id');
        $Message->where($map)->setField('type_result', 1);
        $Message->where($map)->setField('is_read', 1);
        if(empty($pid)){
           $ProjectUser->add(array('project_id'=>$vo['value'], 'user_id'=>$vo['to_userid']));
        }
        echo '1'; 
         
        
        
        
    }

    //拒绝邀请
    public function refuse($id=0){
        $map['id'] = $id;
        $map['to_userid'] = session(C('SESSION_ADMIN_ID'));
        $Message = M('member_message');
        $vo = $Message->where($map)->find();

        //$cond['project_id'] = $vo['value'];
        //$cond['user_id'] = $vo['to_userid'];

        //M('project_user')->where($cond)->delete();
        $Message->where($map)->setField('type_result', -1);
        $Message->where($map)->setField('is_read', 1);
        echo '1';
    }

}
    
    
